/*
 * OREKIT-X
 * Copyright 2002-2008 CS Communication & Systemes
 * 
 * Parts of this software package have been licensed to CS
 * Communication & Systemes (CS) under one or more contributor license
 * agreements.  See the NOTICE file distributed with this work for
 * additional information.
 *  
 * This is an experimental copy of OREKIT from www.orekit.org.
 * Please use the original OREKIT from orekit.org for normal work
 * unrelated to this research project.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License.  You
 * may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */
package ore.errors;

import java.text.MessageFormat;
import java.text.ParseException;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/** 
 * Class of orekit exceptions that are specific to the package, not
 * general ArrayIndexOutOfBoundsException or
 * InvalidParameterException, etc..
 * 
 * Class provides utility methods for localizing messages in cases
 * having translations.  Cases not having translations employ the
 * default message.
 *
 * @author Luc Maisonobe
 */
public class OrekitException
    extends RuntimeException
{

    /** 
     * Translate and format a message.
     * 
     * @param specifier format specifier (to be translated)
     * @param parts parts to insert in the format (no translation)
     * 
     * @return Translated and formatted (localized) message
     */
    public static String translate(String specifier, Object ... parts) {
        String translated;
        try {
            translated = RESOURCES.getString(specifier);
        }
        catch (MissingResourceException mre) {

            translated = specifier;
        }
        return new MessageFormat(translated).format(parts);
    }
    /** 
     * Create an {@link java.lang.IllegalArgumentException} with
     * localized message.
     * 
     * @param specifier format specifier (to be translated)
     * @param parts parts to insert in the format (no translation)
     * 
     * @return An {@link java.lang.IllegalArgumentException} with
     * localized message
     */
    public static IllegalArgumentException createIllegalArgumentException(String specifier,
                                                                          Object ... parts) {
        return new IllegalArgumentException(translate(specifier, parts));
    }
    /** 
     * Create an {@link java.lang.IllegalStateException} with
     * localized message.
     * 
     * @param specifier format specifier (to be translated)
     * @param parts parts to insert in the format (no translation)
     * 
     * @return An {@link java.lang.IllegalStateException} with
     * localized message
     */
    public static IllegalStateException createIllegalStateException(String specifier,
                                                                    Object ... parts) {
        return new IllegalStateException(translate(specifier, parts));
    }
    /** 
     * @param specifier format specifier (to be translated)
     * @param parts parts to insert in the format (no translation)
     * 
     * @return An {@link java.text.ParseException} with localized message.
     */
    public static ParseException createParseException(String specifier,
                                                      Object ... parts) {
        return new ParseException(translate(specifier, parts), 0);
    }
    /** 
     * @param cause Underlying cause
     * 
     * @return An {@link java.lang.RuntimeException} with localized message.
     */
    public static RuntimeException createInternalError(Throwable cause) {
        return new RuntimeException(translate("internal error, contact maintenance at {0}",
                                              "orekit@c-s.fr"),
                                    cause);
    }
    /** 
     * Resources bundle.
     */
    private static final ResourceBundle RESOURCES;
    static {
        RESOURCES = ResourceBundle.getBundle("META-INF/localization/ExceptionsMessages");
    }


    /** 
     * Build an exception with a translated and formatted message
     * @param specifier format specifier (to be translated)
     * @param parts parts to insert in the format (no translation)
     */
    public OrekitException(String specifier, Object ... parts) {
        super(translate(specifier, parts));
    }
    /** 
     * Build an exception from a cause and with a specified message
     * @param message descriptive message
     * @param cause underlying cause
     */
    public OrekitException(String message, Throwable cause) {
        super(message, cause);
    }
    /** 
     * Build an exception from a cause and with a translated and formatted message
     * @param cause underlying cause
     * @param specifier format specifier (to be translated)
     * @param parts parts to insert in the format (no translation)
     */
    public OrekitException(Throwable cause, String specifier, Object ... parts) {
        super(translate(specifier, parts), cause);
    }


    /**
     * @return PropagationException from cause chain or null for not present
     */
    public PropagationException getCausePropagationException(){

        for (Throwable t = this; t != null; t = t.getCause()) {
            if (t instanceof PropagationException) {
                return (PropagationException) t;
            }
        }
        return null;
    }
}
